#!/bin/bash

# 预定义的用户名和密码
username="root"
password="123456"

# 启动netcat服务器
nc -l -p 1234 | while read line
do
    # 分割接收到的行以获取用户名和密码
    received_username=$(echo $line | cut -d':' -f1)
    received_password=$(echo $line | cut -d':' -f2)

    # 检查用户名和密码是否匹配
    if [[ "$received_username" == "$username" && "$received_password" == "$password" ]]; then
        echo "Authentication successful!"
        # 认证成功后，进入一个新的循环以保持通信
        while true
        do
            read input
            # 处理用户的输入，这里可以是聊天逻辑
            echo "Received: $input"
            # 如果用户输入了特定的退出命令，比如 'quit'，则退出循环
            if [[ "$input" == "quit" ]]; then
                break
            fi
        done
    else
        echo "Authentication failed!"
        # 退出当前循环迭代，等待下一次连接
    fi
done
